package com.ithinks.flow.common.utils.middleware.rabbitMQ.Topic;

import com.ithinks.flow.common.utils.middleware.rabbitMQ.ConnectionUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description
 * @ClassName Topic
 * @Author MR.LI
 * @date 2020.11.25 19:32
 */
public class TopicProducer {

    //定义队列名称
    private static final String QUEUE_INFORM_EMAIL = "queue_topic_email";

    private static final String QUEUE_INFORM_SMS = "queue_topic_sms";

    //指定交换机名称
    private static final String EXCHANGE_TOPIC_INFORM ="exchange_topic_inform";

    //连接配置参数
    private static String IP = "localhost";
    //端口
    private static int PORT = 5672;
    //虚拟机IP，默认/
    private static String VHOST = "/";
    //队列名称
    private static String USERNAME = "guest";

    private static String PASSWORD = "guest";


    public static void main(String[] args) {

        Connection connection = ConnectionUtils.getConnection(IP, PORT, VHOST, USERNAME, PASSWORD);
        Channel channel = null;

        try {
            channel = connection.createChannel();
            channel.exchangeDeclare(EXCHANGE_TOPIC_INFORM, BuiltinExchangeType.TOPIC);
            channel.queueDeclare(QUEUE_INFORM_EMAIL,true,false,false,null);
            channel.queueDeclare(QUEUE_INFORM_SMS,true,false,false,null);
            //发送邮件消息
            for(int i= 0 ;i<20; i++)
            {
                String message = "email inform to user"+i;
                channel.basicPublish(EXCHANGE_TOPIC_INFORM,"inform.email",null,message.getBytes());
                System.out.println("send Message is : " + message);
            }
            //发送短信消息
            for(int i= 0 ;i<20; i++)
            {
                String message = "email sms to user"+i;
                channel.basicPublish(EXCHANGE_TOPIC_INFORM,"inform.sms",null,message.getBytes());
                System.out.println("send Message is 001: " + message);
            }
//            //发送短信和邮件消息
//            for (int i=0;i<10;i++){
//                String message = "sms and email inform to user"+i;
//                channel.basicPublish(EXCHANGE_TOPIC_INFORM, "inform.sms.email", null,
//                        message.getBytes());
//                System.out.println("Send Message is:'" + message + "'");
//            }
        }catch(Exception e){
            e.printStackTrace();

        }finally {
            if(channel != null)
            {
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
                ConnectionUtils.closeConnection(connection);
            }
        }
    }
}
